<?php

namespace App\Http\Controllers\Admin\Finance\Receivable;

use App\Exports\ReceivableTakeOrderDetailExport;
use App\Http\Controllers\Admin\Base\ExchangeRate;
use App\Http\Controllers\Controller;
use App\Http\Requests\airline\base\BanRequest;
use App\Models\airline\base\FlightModel as BaseFlightModel;
use App\Models\DictionaryModel;
use App\Models\finance\PayableTakeOrderDetailModel;
use App\Models\finance\PayableTakeOrderModel;
use App\Models\finance\PayableTmpModel;
use App\Models\finance\ReceivableTakeOrderDetailModel;
use App\Models\OrderModel;
use App\Models\TakeOrderModel;
use App\Models\TakeOrderRecModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class TakeOrderDetail extends Controller
{
    use BanRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $take_order_sn = $request->input('take_order_sn', '');
        $name = $request->input('name', '');
        $agent_id = $request->input('agent_id', '');
        $flight_date_start = $request->input('flight_date_start', '');
        $flight_date_end = $request->input('flight_date_end', '');


        $query = ReceivableTakeOrderDetailModel::query()->with(['customer', 'currency', 'take_order']);

        if($take_order_sn){
            $find = TakeOrderModel::query()->where('sn', $take_order_sn)->first();
            $take_order_id = $find->id ?? 0;
            $query->where('take_order_id', $take_order_id);
        }

        if($name){
            $query->where('name', 'like', "%{$name}%");
        }

        if($flight_date_start && $flight_date_end){
            $query->whereBetween('flight_date', [$flight_date_start, $flight_date_end]);
        }


        $total = $query->count('id');
        $total_amount = $query->sum('amount') ?? 0;
        $total_amount_cny = $query->sum('amount_cny') ?? 0;
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {

            $res = [
                'id' => $item->id,
                'receivable_take_order_id' => $item->receivable_take_order_id,
                'customer_id' => $item->customer_id,
                'customer_name' => $item->customer->name ?? '',
                'flight_date' => $item->flight_date,
                'sn' => $item->take_order->sn ?? '',
                'name' => $item->name,
                'number' => $item->number,
                'unit' => $item->unit,
                'price' => $item->price,
                'currency_code' => $item->currency->short_code ?? '',
                'rate' => $item->rate,
                'amount' => $item->amount,
                'amount_cny' => $item->amount_cny,
            ];
            return $res;
        });

        $statistics = [
            'amount' => $total_amount,
            'amount_cny' => $total_amount_cny,
        ];

        return $this->success(['list' => $list, 'total' => $total, 'statistics' => $statistics]);
    }



    public function export(Request $request) {
        $ids = $request->input('ids',  []);
        $details = ReceivableTakeOrderDetailModel::whereIn('id', $ids)->get();

        if(!$details){
            return $this->error('请选择要导出的数据');
        }


        $list = $details->map(function($item) {

            $res = [
                'flight_date' => $item->flight_date,
                'sn' => $item->take_order->sn ?? '',
                'name' => $item->name,
                'number' => $item->number,
                'unit' => $item->unit,
                'price' => $item->price,
                'currency_code' => $item->currency->short_code ?? '',
                'rate' => $item->rate,
                'amount' => $item->amount,
                'amount_cny' => $item->amount_cny,
            ];
            return $res;
        });

        $file_name = '应付明细-'.date('Ymd').'.xlsx';

        return Excel::download(new ReceivableTakeOrderDetailExport($list), $file_name);

    }






}
